home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / programming / utilities / easyproc2.lha / EasyProcess / hc / serial.c < prev   
Encoding:
C/C++ Source or Header  |  1992-11-05  |  4.5 KB  |  239 lines

  1. /*
  2.  *    Serial functions.
  3.  *
  4.  */
  5.  
  6. #include "hc.h"
  7.  
  8. ULONG ReadFlag;
  9.  
  10. static struct MsgPort *ReadPort;
  11. static struct MsgPort *WritePort;
  12. static struct IOExtSer *ReadIO;
  13. static struct IOExtSer *WriteIO;
  14. static UBYTE *ReadBuffer;
  15. static UWORD SerOpened;
  16. static UWORD Synchronize;
  17. static ULONG BaudRate;
  18.  
  19. void InitRead (void)
  20. {
  21.     ReadIO->IOSer.io_Command = CMD_READ;
  22.     ReadIO->IOSer.io_Length = 2;
  23.     ReadIO->IOSer.io_Data = (APTR)ReadBuffer;
  24.     SendIO (ReadIO);
  25. }
  26.  
  27.  
  28. void SyncSer (void)
  29. {
  30.     Synchronize = 1;
  31. }
  32.  
  33.  
  34. void ReadSer (void)
  35. {
  36.     UWORD count;
  37.  
  38.     /*
  39.      *    Wait for data and classify it.  Should always be 2 bytes long,
  40.      *    unless we asked for one byte for synchronisation.
  41.      */
  42.  
  43. DB    Say ("Waiting for IO.\n");
  44.  
  45.     WaitIO (ReadIO);
  46.  
  47. DB    Say ("Storing data.\n");
  48.  
  49.     if (Synchronize != 2)
  50.         ClassData (ReadBuffer, ReadIO->IOSer.io_Actual);
  51.     else
  52.         Synchronize = 0;
  53.  
  54.     if (BaudRate != ReadIO->io_Baud)
  55.     {
  56. DB        Say ("Changing read baud rate.\n");
  57.  
  58.         ReadIO->IOSer.io_Command = SDCMD_SETPARAMS;
  59.         ReadIO->io_Baud = BaudRate;
  60.         DoIO (ReadIO);
  61.     }
  62.  
  63.     while (1)
  64.     {
  65. DB        Say ("Querying amount of data.\n");
  66.  
  67.         ReadIO->IOSer.io_Command = SDCMD_QUERY;
  68.         DoIO (ReadIO);
  69.         if (ReadIO->IOSer.io_Actual < 2)
  70.         {
  71.             if (Synchronize == 1)
  72.             {
  73.                 /*
  74.                  *    If we must get in sync and there's a byte ready, do it
  75.                  *    right away, else we request one byte.
  76.                  */
  77.                 if (ReadIO->IOSer.io_Actual == 1)
  78.                 {
  79.                     ReadIO->IOSer.io_Command = CMD_READ;
  80.                     ReadIO->IOSer.io_Length = 1;
  81.                     ReadIO->IOSer.io_Data = (APTR)ReadBuffer;
  82.                     DoIO (ReadIO);
  83.                     Synchronize = 0;
  84.                 }
  85.                 else
  86.                 {
  87.                     ReadIO->IOSer.io_Command = CMD_READ;
  88.                     ReadIO->IOSer.io_Length = 1;
  89.                     ReadIO->IOSer.io_Data = (APTR)ReadBuffer;
  90.                     SendIO (ReadIO);
  91.                     Synchronize = 2;
  92.                     return;
  93.                 }
  94.             }
  95.             ReadIO->IOSer.io_Command = CMD_READ;
  96.             ReadIO->IOSer.io_Length = 2;
  97.             ReadIO->IOSer.io_Data = (APTR)ReadBuffer;
  98.             SendIO (ReadIO);
  99.             return;
  100.         }
  101.         else
  102.         {
  103.             count = Synchronize + min (ReadIO->IOSer.io_Actual & 0xfffe, 4096);
  104.             ReadIO->IOSer.io_Command = CMD_READ;
  105.             ReadIO->IOSer.io_Length = count;
  106.             ReadIO->IOSer.io_Data = (APTR)ReadBuffer;
  107.             DoIO (ReadIO);
  108.             ClassData (ReadBuffer + Synchronize, count - Synchronize);
  109.             Synchronize = 0;
  110.         }
  111.     }
  112. }
  113.  
  114.  
  115. void WriteSer (UBYTE *Buffer, UWORD Size)
  116. {
  117.     if (!CheckIO (WriteIO))
  118.         WaitIO (WriteIO);
  119.  
  120.     if (BaudRate != WriteIO->io_Baud)
  121.     {
  122.         WriteIO->IOSer.io_Command = SDCMD_SETPARAMS;
  123.         WriteIO->io_Baud = BaudRate;
  124.         DoIO (WriteIO);
  125.     }
  126.  
  127.     WriteIO->IOSer.io_Command = CMD_WRITE;
  128.     WriteIO->IOSer.io_Length = Size;
  129.     WriteIO->IOSer.io_Data = (APTR)Buffer;
  130.     SendIO (WriteIO);
  131. }
  132.  
  133.  
  134. void FlushWriteSer (void)
  135. {
  136.     WaitIO (WriteIO);
  137. }
  138.  
  139.  
  140. BOOL OpenSer (void)
  141. {
  142.     if (ReadPort)
  143.         return 1;
  144.  
  145.     ReadBuffer = AllocMem (4096, 0L);
  146.     if (ReadBuffer != NULL)
  147.     {
  148.         ReadPort = CreatePort (0, 0);
  149.         if (ReadPort != NULL)
  150.         {
  151.             WritePort = CreatePort (0, 0);
  152.             if (WritePort != NULL)
  153.             {
  154.                 ReadIO = (struct IOExtSer *)CreateExtIO (ReadPort, sizeof (struct IOExtSer));
  155.                 if (ReadIO != NULL)
  156.                 {
  157.                     WriteIO = (struct IOExtSer *)CreateExtIO (WritePort, sizeof (struct IOExtSer));
  158.                     if (WriteIO != NULL)
  159.                     {
  160.                         if (!OpenDevice ("serial.device", 0, ReadIO, 0))
  161.                         {
  162.                             SerOpened = 1;
  163.                             CopyMem (ReadIO, WriteIO, sizeof (struct IOExtSer));
  164.                             WriteIO->IOSer.io_Message.mn_ReplyPort = WritePort;
  165.                             WriteIO->IOSer.io_Command = SDCMD_SETPARAMS;
  166.                             WriteIO->io_Baud = 9600;
  167.                             WriteIO->io_ReadLen = 8;
  168.                             WriteIO->io_WriteLen = 8;
  169.                             WriteIO->io_StopBits = 1;
  170.                             WriteIO->io_SerFlags &= ~SERF_PARTY_ODD;
  171.                             DoIO (WriteIO);
  172.                             ReadIO->IOSer.io_Command = SDCMD_SETPARAMS;
  173.                             ReadIO->io_Baud = 9600;
  174.                             ReadIO->io_ReadLen = 8;
  175.                             ReadIO->io_WriteLen = 8;
  176.                             ReadIO->io_StopBits = 1;
  177.                             ReadIO->io_SerFlags &= ~SERF_PARTY_ODD;
  178.                             DoIO (ReadIO);
  179.                             ReadFlag = 1L << ReadPort->mp_SigBit;
  180.                             BaudRate = 9600;
  181.                             return 1;
  182.                         }
  183.                     }
  184.                 }
  185.             }
  186.         }
  187.     }
  188.  
  189.     CloseSer ();
  190.     return 0;
  191. }
  192.  
  193.  
  194.  
  195. void SetBaud (UWORD Baud)
  196. {
  197.     BaudRate = Baud;
  198. }
  199.  
  200.  
  201. UWORD GetBaud (void)
  202. {
  203.     return (UWORD)BaudRate;
  204. }
  205.  
  206.  
  207. void CloseSer (void)
  208. {
  209.     if (SerOpened)
  210.     {
  211.         if (ReadIO) StopIO (ReadIO);
  212.         if (WriteIO) StopIO (WriteIO);
  213.         CloseDevice (ReadIO);
  214.     }
  215.     if (ReadIO) DeleteExtIO (ReadIO);
  216.     if (WriteIO) DeleteExtIO (WriteIO);
  217.     if (ReadPort) DeletePort (ReadPort);
  218.     if (WritePort) DeletePort (WritePort);
  219.     if (ReadBuffer) FreeMem (ReadBuffer, 4096);
  220.  
  221.     ReadIO = NULL;
  222.     WriteIO = NULL;
  223.     ReadPort = NULL;
  224.     WritePort = NULL;
  225.     SerOpened = 0;
  226.     ReadFlag = 0;
  227.     ReadBuffer = NULL;
  228. }
  229.  
  230.  
  231. void StopIO (struct IOExtSer *IO)
  232. {
  233.     Forbid ();
  234.     if (!CheckIO (IO))
  235.         AbortIO (IO);
  236.     Permit ();
  237.     WaitIO (IO);
  238. }
  239.